package ema2;

import java.util.ArrayList;
//import java.util.HashMap;
//import ema2.Weekday.Day;

public class NewReport {

        private String type;
	private Schedule schedule;
        private int day;
        private String[] week;
	//private HashMap<String, Object> options;

	public NewReport(String type, Schedule schedule/*, HashMap<String, Object> options*/) {
		this.schedule = schedule;
		//this.options = options;
                this.type = type;
                this.week = new String[7];
                week[0] = "Monday";
                week[1] = "Tuesday";
                week[2] = "Wednesday";
                week[3] = "Thursday";
                week[4] = "Friday";
                week[5] = "Saturday";
                week[6] = "Sunday";
	}

        public NewReport(String type, Schedule schedule, int day) {
                new NewReport(type, schedule);
                this.day = day;
	}

        @Override
        public String toString() {
                if (type.contains("DAY")) return dayReport();
                if (type.contains("WEEK")) return weekReport();
		if (type.contains("FULL")) return fullReport();
                else return null;
        }

	private String dayReport() {

            ArrayList<Event> events = schedule.getEventList(this.day);
		/*if (this.options.containsKey("day")){
			String res = "";
			Weekday.Day weekday = (Day)this.options.get("day");
			ArrayList<Event> events = this.schedule.getSchedule().get(weekday);*/

			if (events == null) {
				return null;
			}
                        String res = "";
			res += week[this.day]/*Weekday.longNameMap.get(weekday)*/ + ":\n------\n";

			for (Event e : events){
				if(e.getTitle().equals("")){
					res += "<no title for this event>";
				}
				else{
					res += e.getTitle();
				}

				//event must always have start time and end time
                                int start = e.getStartTime();
                                int end = e.getEndTime();
				res += "\nat " + start + " to " + end;

				if (e.getLocation().equals("")){
					res += "\n<no location specified>";
				}
				else {
					res += "\nin " + e.getLocation();
				}
				res += "\n\n";
			}
			return res;
		//}

		//return null;
        }

	private String weekReport() {

		//if (this.options.containsKey("days")){
			//ArrayList<Weekday.Day> days = (ArrayList<Day>)this.options.get("days");
			String[][] res = new String[/*days.size()*/7 + 1][7]; // +1 for header row

			res[0][0] = "\t";

			for (int i = 1, j = 0; j < Event.VALID_START_TIMES.length; i++, j++){
				res[0][i] = Event.VALID_START_TIMES[j] + "\t";
			}

			int i = 1;
			//for (Day day : days){
                        for (int j = 0; j<7; j++) {
				res[i][0] = week[j]/*day.toString()*/ + "\t";
				i++;
			}

			i = 1;
			//for (Day d : days){
                        for (int j = 0; j < 7; j++) {
				ArrayList<Event> events = this.schedule.getEventList(j);//getSchedule().get(d);

				if (events == null){
					return null;
				}
				else if (events.size() == 0){
					for (int x = 1; x < 7; x++) {
						res[i][x] = "\t";
					}
				}

				for (Event event : events){
					String entry = "\t"; // if event is null

					if (event.getLocation() != null) {
					  entry = event.getLocation()+"\t";
					}

					if (event.getStartTime() == 8)     { res[i][1] = entry; }
					else if(event.getStartTime() == 10) { res[i][2] = entry; }
					else if(event.getStartTime() == 12) { res[i][3] = entry;	}
					else if(event.getStartTime() == 14) { res[i][4] = entry; }
					else if(event.getStartTime() == 16) { res[i][5] = entry; }
					else if(event.getStartTime() == 18) { res[i][6] = entry; }

					// fill up with empties
					for (int x = 1; x < 7; x++) {
						if (res[i][x] == null){
							res[i][x] = "\t";
						}
					}

				}
				i++;
			}

			String response = "";

			for (int j = 0; j < res.length; j++){
				for (int k = 0; k < res[0].length; k++){
					response += res[j][k];
				}
				response += "\n";
			}

			return response;
		//}
		//return null;
	}

	private String fullReport(){
		String res = "";
		
		//for (Weekday.Day day : schedule.getSchedule().keySet()){
                for (int j = 0; j < 7; j++) {
			res += week[j]/*day*/ + ":\n";
			res += "----\n";
			for (Event event : schedule.getEventList(j)/*getSchedule().get(day)*/){
				res += event.getTitle();
				res += "\nat " + event.getStartTime() + "-" + event.getEndTime();
				res += "\nin " + event.getLocation();
			}
			res += "\n\n";
		}
		
		return res;
	}

}
